package cn.jingdalei.pet.controller;

import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.basic.util.LoginContext;
import cn.jingdalei.pet.domain.SearchMasterMsg;
import cn.jingdalei.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

    /**
     *
     * @param searchMasterMsg // 后台传来的发布信息
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,
                              HttpServletRequest request){
        // 获取当前用户 通过loginContext
        try {
            // LoginContext.currentLogin(request).getId()获取当前登录人的id
            seachMasterMsgService
                    .publish(searchMasterMsg, LoginContext.currentLogin(request).getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试");
        }

    }
}
